func asyncWait<%= sprintf("%s%s", resource_name, gasync.nil? ? titlelize(api.name) : "") %>(d *schema.ResourceData, config *Config, result interface{},
   client *golangsdk.ServiceClient, timeout time.Duration) (interface{}, error) {

<%
   async = gasync.nil? ? api.async : gasync
   async_op = async.operation
-%>
<% cl = "client" -%>
<% if (!gasync.nil?) || need_build_async_url(api) -%>
<%   if async_op.path_parameter -%>
    data := make(map[string]interface{})
    pathParameters := map[string][]string {
<%     async_op.path_parameter.each do |k, v| -%>
<%=      sprintf("\"%s\": []string{%s},", k, index2navigate(v))  %>
<%     end -%>
    }
    for key, path := range pathParameters {
        value, err := navigateValue(result, path, nil)
        if err != nil {
            return nil, fmt.Errorf("Error retrieving async operation path parameter, err=%s", err)
        }
        data[key] = value
    }

    url, err := replaceVars(d, "<%= async_op.path -%>", data)
    if err != nil {
        return nil, err
    }
    url = <%= cl %>.ServiceURL(url)
<%   elsif async_op.path.include?("{") -%>
    url, err := replaceVars(d, "<%= async_op.path -%>", nil)
    if err != nil {
        return nil, err
    }
    url = <%= cl %>.ServiceURL(url)
<%   else -%>
    url := <%= cl %>.ServiceURL(<%= sprintf("\"%s\"", async_op.path) %>)
<%   end -%>
<% end -%>

<% rscodes = async_op.success_codes -%>
<% status_check = async.status_check -%>
<% if status_check -%>
    return waitToFinish(
        []string{<%= status_check.complete.compact.map { |x| "\"#{x}\"" }.join(", ") %>},
        []string{<%= status_check.pending.compact.map { |x| "\"#{x}\"" }.join(", ") %>},
        timeout, <%= sprintf('%d', async.operation.wait_ms / 1000) %> * time.Second,
        func() (interface{}, string, error) {
            r := golangsdk.Result{}
            _, r.Err = <%= cl %>.Get(url, &r.Body, &golangsdk.RequestOpts{
              <% unless rscodes.nil? -%>
                OkCodes: []int{<% rscodes.each_index do |i| -%><%= rscodes[i] %><%= (i + 1) == rscodes.length ? "" : "," %><% end -%>},
              <% end -%>
              <% if async_op.header_params.nil? -%>
                MoreHeaders: map[string]string{"Content-Type": "application/json"}})
              <% else -%>
                MoreHeaders: map[string]string{
                    "Content-Type": "application/json",
                <% async_op.header_params.each do |k, v| -%>
                    <%= sprintf("\"%s\": \"%s\",", k, v)%>
                <% end -%>
                }})
              <% end -%>
            if r.Err != nil {
                return nil, "", nil
            }

            status, err := navigateValue(r.Body, []string{<%= index2navigate(status_check.field) %>}, nil)
            if err != nil {
                return nil, "", nil
            }
            return r.Body, convertToStr(status), nil
        },
    )
<% elsif async.custom_status_check_func -%>
    return waitToFinish(
        []string{"Done"}, []string{"Pending"}, timeout, <%= sprintf('%d', async.operation.wait_ms / 1000) %> * time.Second,
        func() (interface{}, string, error) {
            r := golangsdk.Result{}
            _, r.Err = <%= cl %>.Get(url, &r.Body, &golangsdk.RequestOpts{
              <% unless rscodes.nil? -%>
                OkCodes: []int{<% rscodes.each_index do |i| -%><%= rscodes[i] %><%= (i + 1) == rscodes.length ? "" : "," %><% end -%>},
              <% end -%>
              <% if async_op.header_params.nil? -%>
                MoreHeaders: map[string]string{"Content-Type": "application/json"}})
              <% else -%>
                MoreHeaders: map[string]string{
                    "Content-Type": "application/json",
                <% async_op.header_params.each do |k, v| -%>
                    <%= sprintf("\"%s\": \"%s\",", k, v)%>
                <% end -%>
                }})
              <% end -%>
            if r.Err != nil {
                return nil, "", nil
            }

            if <%= async.custom_status_check_func %>(r.Body) {
                return r.Body, "Done", nil
            }
            return r.Body, "Pending", nil
        },
    )
<% else -%>
<%   raise "Must sepecify status check for api: " + (gasync.nil? ? api.name : "global async") -%>
<% end -%>
}
